package com.itheima.blackitheima;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
//迭代器遍历集合
public class IteratorTest {
    public static void main(String[] args) {
        //创建集合
        Collection<String> collection=new ArrayList<>();
        collection.add("aaa");
        collection.add("bbb");
        collection.add("ccc");
        collection.add("ddd");
        //遍历集合  hasNext:判断是否有元素   next:提取元素并指针后移
        //利用死循环 不断判断,并且输出
        //创建迭代器对象  遍历集合
        Iterator<String> it = collection.iterator();
        while(it.hasNext()){
            String next = it.next();
            System.out.print(next+" ");
        }
        System.out.println();

        //使用迭代器遍历集合,删除指定元素
        System.out.println("删除aaa后的集合:");
        Iterator<String> it1 = collection.iterator();
        while(it1.hasNext()){
            String next = it1.next();
            if("aaa".equals(next)){
                it1.remove();
                continue;
            }
            System.out.print(next+" ");
        }
        System.out.println();

        System.out.println("---------");

        //增强for遍历集合
        for (String s : collection) {
            System.out.println(s);
        }
    }
}
